package com.kfc.order.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.kfc.order.dao.CustomerDao;
import com.kfc.order.util.DBUtil;
import com.kfc.order.vo.Address;
import com.kfc.order.vo.Customer;
import com.kfc.order.vo.OrderForm;

@Repository("cd")
public class CustomerDaoImpl implements CustomerDao {
	@PersistenceContext(name = "un")
	private EntityManager em;

	// ��ӹ˿�
	@Override
	public int addCustomer(Customer customer) {
		em.persist(customer);
		return customer.getCustomerid();
	}

	// ���ҹ˿�
	@Override
	public Customer findCustomer(String customertel, String customerpsd) {

		String jpql = "select c from Customer c where c.customertel=:customertel and c.customerpsd=:customerpsd";
		List<Customer> cus = em.createQuery(jpql)
				.setParameter("customertel", customertel)
				.setParameter("customerpsd", customerpsd).getResultList();

		if (cus.isEmpty()) {
			return null;
		}else{
			return cus.get(0);
		}
	}

	// ������ϵ��ʽ�Ƿ��Ѵ���
	@Override
	public Boolean findTel(String customertel) {
		String jpql = "select c from Customer c where c.customertel=:customertel";
		List<Customer> cus = em.createQuery(jpql)
				.setParameter("customertel", customertel).getResultList();
		if (cus.isEmpty()) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public int addAddress(Address address) {
		// TODO Auto-generated method stub
		return 0;
	}





}
